package xhwl.insurance.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.trello.rxlifecycle.components.RxActivity;

import org.w3c.dom.Text;

import java.util.List;

import xhwl.insurance.R;
import xhwl.insurance.customView.PlayerWebView;
import xhwl.insurance.http.api.GiveThumbs_upApi;
import xhwl.insurance.http.bean.resp.ErrorCode;
import xhwl.insurance.http.bean.resp.VideoListResp;
import xhwl.insurance.http.bean.resp2.HttpResp;
import xhwl.insurance.ui.fragment.FindFragment2;
import xhwl.insurance.util.ToastUtil;
import xhwl.pulltorefresh.adapter.BaseAdapter;
import xhwl.pulltorefresh.adapter.ViewHolder;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

/**
 * Created by admin on 2017-09-29.
 */

public class FindAdapter2 extends BaseAdapter<VideoListResp> {
    private static final int DEFAULT_PLAYING_INDEX = -1;
    OnItemClickLitener onItemClickLitener;
    PlayerWebView curVideoPlay;
    int playingIndex = DEFAULT_PLAYING_INDEX;

    public FindAdapter2(Context context, List<VideoListResp> datas) {
        super(context, R.layout.adapter_find, datas);

    }

    @Override
    public void convert(final ViewHolder holder, final VideoListResp video) {
        holder.setText(R.id.tv_find_title, video.getVIDEO_NAME());
        holder.setText(R.id.tv_find_time, video.getCREATE_DATE());
        // 如果设置了回调，则设置点击事件
        final int pos = holder.getLayoutPosition();
        holder.setText(R.id.tv_find_goodnum, video.getTHUMBS_UP());
        final TextView textview = holder.getView(R.id.tv_find_goodnum);
        final ImageView imageview = (ImageView) holder.getView(R.id.iv_find_good);
        final PlayerWebView videoPlay = holder.<PlayerWebView>getView(R.id.videoWebView);

        if (video.getTHUMBS_FLAG().equals("1")) {
            imageview.setImageResource(R.drawable.icon_thumbs_up2);
        } else {
            imageview.setImageResource(R.drawable.icon_thumbs_up);
        }

        videoPlay.setCoverImageUrl(video.getVIDEO_PIC());
        videoPlay.setVideoUrl(video.getVIDEO_URL());

        if (onItemClickLitener != null) {
            holder.setOnclickListener(R.id.llItem, new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    onItemClickLitener.onItemClick(pos - 1);
                }
            });

            holder.setOnclickListener(R.id.iv_find_good, new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    onItemClickLitener.onGoodClick(pos - 1, imageview, textview);

                }
            });
            holder.setOnclickListener(R.id.iv_videoshare, new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    onItemClickLitener.onShareClick(pos - 1,videoPlay);
                }
            });

            videoPlay.setCallBack(new PlayerWebView.CallBack() {
                @Override
                public void onPlayBtnClick() {
                    onItemClickLitener.onClickAmount(pos - 1);
                    pauseCurrentVideo();
                    videoPlay.play();
                    curVideoPlay = videoPlay;
                    playingIndex = pos;
                }
            });

        }
    }

    public boolean isPlaying() {
        return curVideoPlay != null && playingIndex != DEFAULT_PLAYING_INDEX;
    }

    public int getPlayingIndex() {
        return playingIndex;
    }

    public void pauseCurrentVideo() {
        if (curVideoPlay != null) {
            curVideoPlay.pause();
            curVideoPlay = null;
            playingIndex = DEFAULT_PLAYING_INDEX;
        }
    }

    public interface OnItemClickLitener {
        void onItemClick(int position);

        void onShareClick(int position,PlayerWebView videoPlay);

        void onGoodClick(int position, ImageView imageview, TextView textview);

        void onClickAmount(int position);
    }

    public void setOnItemClickLitener(OnItemClickLitener onItemClickLitener) {
        this.onItemClickLitener = onItemClickLitener;
    }
}
